package com.mall.controller;

import com.mall.result.Result;
import com.mall.domain.dto.AddressDTO;
import com.mall.domain.vo.AddressVO;
import com.mall.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @date 2024/11/22 20:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
@Tag(name = "地址管理", description = "地址管理相关接口")
public class AddressController {
    private final AddressService addressService;

    /**
     * 添加地址
     * @param addressDTO 地址传输对象
     * @return 添加成功
     */
    @PostMapping("/add")
    @Operation(summary = "添加地址")
    public Result<String> addAddress(@RequestBody AddressDTO addressDTO){
        addressService.addAddress(addressDTO);
        return Result.success("添加成功");
    }

    /**
     * 查询地址列表
     * @return 地址列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询地址列表")
    public Result<List<AddressVO>> listAddress(){
        List<AddressVO> addressList = addressService.listAddress();
        return Result.success(addressList);
    }

    /**
     * 查询某个地址详情
     * @param id 地址id
     * @return 地址详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询某个地址详情")
    public Result<AddressVO> getAddressById(@PathVariable Long id){
        AddressVO address = addressService.getAddressById(id);
        return Result.success(address);
    }

    /**
     * 修改地址
     * @param addressDTO 地址传输对象,修改内容
     * @param id 地址id
     * @return 修改成功
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "修改地址")
    public Result<String> updateAddress(
            @RequestBody AddressDTO addressDTO, @PathVariable Long id){
        addressService.updateAddress(addressDTO, id);
        return Result.success("修改成功");
    }

    /**
     * 删除地址
     * @param id 地址id
     * @return 删除成功
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除地址")
    public Result<String> deleteAddress(@PathVariable Long id){
        addressService.deleteAddress(id);
        return Result.success("删除成功");
    }
}
